
export function randomNum(len, radix) {
  const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  const uuid = []
  radix = radix || chars.length

  if (len) {
    // Compact form
    for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix ]
  } else {
    // rfc4122, version 4 form
    let r

    // rfc4122 requires these characters
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'

    // Fill in random data.  At i==19 set the high bits of clock sequence as
    // per rfc4122, sec. 4.1.5
    for (let i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | Math.random() * 16
        uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]
      }
    }
  }
  return uuid.join('') + new Date().getTime()
}


export function formatNum (num) {
  return (+(+num || 0).toFixed(3)).toLocaleString()
}

export function eachTree(tree = [], childrenKey = 'children', callback, parent) {
  tree.forEach(item => {
    if (typeof callback === 'function') {
      callback(item, parent)
      eachTree(item[childrenKey], childrenKey, callback, item)
    }
  })
}

export function horizontalScroll(ele, targetLeft = 0, scrollTime = 150) {
  if (ele) {
    let scrollLeft = ele.scrollLeft
    const timeout = 6
    const scrollTimes = scrollTime / timeout
    const scrollHeight = (Math.abs(scrollLeft - targetLeft) / scrollTimes) || 1
    const scrollDirection = targetLeft < scrollLeft ? 'up' : 'down'
    const timer = setInterval(() => {
      if (scrollDirection === 'up') {
        scrollLeft = scrollLeft - scrollHeight
        if (scrollLeft <= targetLeft) {
          scrollLeft = targetLeft
          clearInterval(timer)
        }
      } else {
        scrollLeft = scrollLeft + scrollHeight
        if (scrollLeft >= targetLeft) {
          scrollLeft = targetLeft
          clearInterval(timer)
        }
      }
      ele.scrollLeft = scrollLeft
    }, timeout)
  }
}

export function verticalScroll(ele, targetTop = 0, scrollTime = 150) {
  if (ele) {
    let scrollTop = ele.scrollTop
    const timeout = 10
    const scrollTimes = scrollTime / timeout
    const scrollHeight = (Math.abs(scrollTop - targetTop) / scrollTimes) || 1
    const scrollDirection = targetTop < scrollTop ? 'up' : 'down'
    const timer = setInterval(() => {
      if (scrollDirection === 'up') {
        scrollTop = scrollTop - scrollHeight
        if (scrollTop <= targetTop) {
          scrollTop = targetTop
          clearInterval(timer)
        }
      } else {
        scrollTop = scrollTop + scrollHeight
        if (scrollTop >= targetTop) {
          scrollTop = targetTop
          clearInterval(timer)
        }
      }
      ele.scrollTop = scrollTop
    }, timeout)
  }
}

export function copyTextToClipboard(text = '') {
  const input = document.createElement('input')
  input.value = text
  document.body.appendChild(input)
  input.select()
  const isSuccessful = document.execCommand('copy')
  document.body.removeChild(input)
  return isSuccessful
}

export function numberWithCommas(num) {
  num = Number(num)
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

export function addZero (value) {
  if (value < 10) {
    return `0${value}`
  }
  return value
}

export function parseToUTCDate (date) {
  if (!(date instanceof Date)) {
    date = new Date(date)
  }
  return `${addZero(date.getUTCDate())}/${addZero(date.getUTCMonth() + 1)}/${date.getUTCFullYear()} ${addZero(date.getUTCHours())}:${addZero(date.getUTCMinutes())}:${addZero(date.getUTCSeconds())}`
}

export function parseToLocaleDate (date) {
  if (!(date instanceof Date)) {
    date = new Date(date)
  }
  return date.toLocaleString()
}

export function measureCpuUsage() {
  const startTime = performance.now();
  let sum = 0;
  for (let i = 0; i < 1000000; i++) {
      sum += Math.random();
  }
  const endTime = performance.now();
  const duration = endTime - startTime;
  console.log(`Execution time: ${duration} ms`);
}

